<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">类型名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="typeName" class="layui-input" type="text" placeholder="请输入类型名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" layui-form-keyDownSearch="true" lay-submit>查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="unitTypeBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="unitTypeTable" lay-filter="unitTypeTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="unitTypeTableBar">
<!--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="unitTypeForm">
    <form lay-filter="unitTypeForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>类型名称</label>
            <div class="layui-input-block typeName-block">
                <input name="typeName" placeholder="请输入类型名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>类型编码</label>
            <div class="layui-input-block">
                <input name="typeCode" placeholder="请输入类型编码" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>类型性质</label>
            <div class="layui-input-block type-block">
                <select name="type">
                        <option value="supplier">供应商</option>
                        <option value="client">客户</option>
                        <option value="staff">员工</option>
                        <option value="channel">渠道</option>
                        <option value="shareholder">股东</option>
                        <option value="other">其他</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>是否启用</label>
            <div class="layui-input-block">
                <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
               <!-- <input type="radio" name="isDeleted" id="start" value="0" title="启用" checked="">
                <input type="radio" name="isDeleted" id="stop" value="1" title="停用" >-->
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入备注" maxlength="200" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right saveBtn">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="unitTypeFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#unitTypeTable',
            url: config.base + 'enterprisecab/unit/admin/unitType/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}
                // ,{field: 'id', sort: true, title: 'ID'}
                ,{type: 'numbers', title: '序号'}
                ,{field: 'typeName', sort: true, title: '类型名称'}
                ,{field: 'typeCode', sort: true, title: '类型编码'}
                ,{field: 'typeDesc', sort: true, title: '单位类型'}
                ,{field: 'remark', sort: true, title: '备注'}
                ,{field: 'creatorName', sort: true, title: '创建人'}
                ,{field: 'createTime', sort: true, title: '创建时间'}
                ,{align: 'center', toolbar: '#unitTypeTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮（暂时无用，删除）
                var menuButton = config.getMenuButton(location.hash);

            }
        });

        // 工具条点击事件
        table.on('tool(unitTypeTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                data.eventType='edit';
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }else if (obj.event === 'detail'){
                data.eventType='detail';
                showEditModel(data);
            }
        });

        //监听排序
        table.on('sort(unitTypeTable)', function(obj){
            table.reload('unitTypeTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            insTb.reload({where: admin.resetSearch(data.field), page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#unitTypeBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            var title='';
            if(data==undefined){
                title='添加往来单位类型';
            }else if(data&&data.eventType=='detail'){
                title='往来单位类型详情';
            }
            admin.open({
                type: 1,
                area: '640px',
                offset: '65px',
                title: title,
                content: $('#unitTypeForm').html(),
                success: function () {
                    if (data){
                        var option = $("form select[name='type']").find("option");
                        layui.each(option, function(index, item){
                            if(data.type  == item.text){
                                data.type = item.value;
                            }
                        });
                    }

                    if(data&&data.eventType=='detail'){
                        $(".saveBtn").find('*').hide();
                        $(".model-form").find('*').attr('disabled','disabled');
                        $(".model-form input").attr('readonly',true);
                        $(".model-form select").attr('readonly',true);
                    }
                    if (data&&data.eventType=='edit'){
                        //$(".model-form").find('*').attr('disabled','disabled');
                        $(".typeName-block input").attr('readonly',true);
                        $(".type-block select").attr('disabled','disabled');
                    }

                    form.val('unitTypeForm', data);
                    // 表单提交事件
                    form.on('submit(unitTypeFormSubmit)', function (d) {
                        layer.load(2);
                        var isDeleted=$("form input[name='isDeleted']").prop("checked");
                        if(!isDeleted){
                            d.field.isDeleted=false;
                        }else{
                            d.field.isDeleted=true;
                        }

                        admin.req(data ? 'enterprisecab/unit/admin/unitType/update' : 'enterprisecab/unit/admin/unitType/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('unitTypeTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprisecab/unit/admin/unitType/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#unitTypeBtnExport').click(function () {
            var checkRows = table.checkStatus('unitTypeTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>